package com.example.controller;
import com.example.result.Result;
import com.example.entity.Category;
import com.example.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;



@RestController
@RequestMapping("/Category")
@Tag(name="类别")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Operation(summary = "查询所有商品分类", description = "查询所有商品分类")
    @GetMapping("/selectAll")
    public Result<List<Category>> selectAll() {
        List<Category> list = categoryService.selectAll();
        return Result.success(list);
    }

    @Operation(summary = "查询所有商品一级分类", description = "查询所有商品一级分类")
    @GetMapping("/selectParent")
    public Result selectParent() {
        List<Category> list = categoryService.selectAll();
        // 使用Stream API提取不重复的parentName
        Set<String> uniqueParentNames = list.stream()
                .map(Category::getParentName)
                .filter(parentName -> parentName != null)
                .collect(Collectors.toSet());

        // 创建一个List用于存放最终的JSON对象形式的数据
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (String name : uniqueParentNames) {
            // 以"parentName: " + parentName作为键，原始的parentName作为值存入Map
            Map<String, Object> item = new HashMap<>();
            item.put("parentName", name);
            resultList.add(item);
        }
        return Result.success(resultList);
    }
}
